package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.Dto.SmsFlashPromotionProductRelationDto;
import com.itheima.client.BrandClient;
import com.itheima.common.domain.AjaxResult;
import com.itheima.common.domain.PageInfo;
import com.itheima.entity.PmsProduct;
import com.itheima.service.ISmsFlashPromotionProductRelationService;
import com.itheima.sms.entity.SmsFlashPromotionProductRelation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/flashProductRelation")
public class SmsFlashPromotionProductRelationController {

    @Autowired
    private ISmsFlashPromotionProductRelationService smsFlashPromotionProductRelationService;

    @Autowired
    private BrandClient brandClient;

    /**
     * 批量选择商品添加关联
     *
     * @param relationList
     * @return
     */
    @PostMapping("/create")
    public AjaxResult create(@RequestBody List<SmsFlashPromotionProductRelation> relationList) {
        smsFlashPromotionProductRelationService.saveBatch(relationList);
        return AjaxResult.success();
    }

    /**
     * 删除关联
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        smsFlashPromotionProductRelationService.removeById(id);
        return AjaxResult.success();
    }

    /**
     * 分页查询不同场次关联及商品信息
     *
     * @param flashPromotionId
     * @param flashPromotionSessionId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(@RequestParam("flashPromotionId") Integer flashPromotionId,
                           @RequestParam("flashPromotionSessionId") Integer flashPromotionSessionId,
                           @RequestParam(value = "pageNum", required = false) Integer pageNum,
                           @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        Page<SmsFlashPromotionProductRelation> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<SmsFlashPromotionProductRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SmsFlashPromotionProductRelation::getFlashPromotionId, flashPromotionId)
                .eq(SmsFlashPromotionProductRelation::getFlashPromotionSessionId, flashPromotionSessionId);
        smsFlashPromotionProductRelationService.page(page, lqw);

        //将查询到的数据传入Dto
        List<SmsFlashPromotionProductRelation> list = page.getRecords();
        List<SmsFlashPromotionProductRelationDto> collect = list.stream().map((item) -> {
            //将product加入dto
            PmsProduct product = brandClient.findById(item.getId());
            SmsFlashPromotionProductRelationDto dto = new SmsFlashPromotionProductRelationDto();
            BeanUtils.copyProperties(item, dto);
            dto.setProduct(product);
            return dto;
        }).collect(Collectors.toList());

        PageInfo pageInfo = PageInfo.toPageList(page);

        return AjaxResult.success(pageInfo);
    }

    /**
     * 修改关联信息
     * @param id
     * @param relation
     * @return
     */
    @PostMapping("/update/{id}")
    public AjaxResult update(@PathVariable Integer id,@RequestBody SmsFlashPromotionProductRelation relation){
        smsFlashPromotionProductRelationService.updateById(relation);
        return AjaxResult.success();
    }

    /**
     * 获取管理商品促销信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult update(@PathVariable Integer id) {
        SmsFlashPromotionProductRelation byId = smsFlashPromotionProductRelationService.getById(id);
        return AjaxResult.success(byId);
    }
}
